package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.service.ITravelTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Reference
    private IStrategyService strategyService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisVOService statisVOService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyTemplateSearchService searchService;

    @RequestMapping("")
    public String index(Model model){
        List<StrategyCommend> top5 = strategyCommendService.getTop5();
        model.addAttribute("commends",top5);
        //hotCds
        List<StrategyStatisVO>hotCds=statisVOService.getHotTp10();
        model.addAttribute("hotCds",hotCds);
        //abroadCds 国外
        List<StrategyStatisVO> abroadCds =statisVOService.getAbroadCdsZsetTop10();
        model.addAttribute("abroadCds",abroadCds);
        //unabroadCds 国内
        List<StrategyStatisVO> unabroadCds =statisVOService.getUnAbroadCdsZsetTop10();
        model.addAttribute("unabroadCds",unabroadCds);
        //themeCds

        //themeCds
        List<Map<String, Object>> themeCds = searchService.queryThemeTop10();
        model.addAttribute("themeCds", themeCds);
        //themes
        List<StatisVO> themes = searchService.queryConditionThemeGroup();
        model.addAttribute("themes", themes);
        //abroads
        List<StatisVO> abroads = searchService.queryConditionAbroadsGroup();
        model.addAttribute("abroads", abroads);
        //abroads
        List<StatisVO> chinas = searchService.queryConditionProvincesGroup();
        model.addAttribute("chinas", chinas);
        return "strategy/index";
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo")SearchQueryObject qo) {
        Page page = searchService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }

    @RequestMapping("/detail")
    public String detail(Model model,Long id){
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail",detail );
        //初始化阅读数
        statisVOService.increaseViewNum(id,1);
        StrategyStatisVO vo=statisVOService.getStatisVO(id);
        statisVOService.addHotScore(id,1);
        model.addAttribute("vo",vo);
        return "strategy/detail";
    }
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyDetailQuery qo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //中国
        Destination dest = toasts.remove(toasts.size() - 1);
        //toasts
        model.addAttribute("toasts", toasts);
        //dest
        model.addAttribute("dest", dest);

        //tags
        List<StrategyTag> list = strategyTagService.list();
        model.addAttribute("tags", list);
        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "strategy/list";
    }
    //攻略评论添加
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyCommentService.saveOrUpdate(comment);
        //评论数加1
        statisVOService.increaseReplyNum(comment.getDetailId(),1);
        int replynum = statisVOService.getStatisVO(comment.getDetailId()).getReplynum();
        statisVOService.addHotScore(comment.getDetailId(),1);
        return AjaxResult.SUCCESS.addData(replynum);
    }

    //评论的列表
    @RequestMapping("/comment")
    public String commentPage(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){
        model.addAttribute("page",strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }
    //toId : 评论的id
    //fromid: 点赞/取消点赞用户的id

    //评论的点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, @UserParam UserInfo userInfo){
        //点赞  与 取消点赞
        if (userInfo==null){
            throw new LogicException("请先登录");
        }
        strategyCommentService.commentThumbUp(toid, userInfo.getId());
        return AjaxResult.SUCCESS;
    }
    //攻略收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo){

        if(userInfo == null){  //登录处理
            AjaxResult result  = new AjaxResult(false, "请先登录");
            result.setCode(102);  //102 事先约定好未登录状态码
            return result;
        }
        //收藏与取消收藏, 根据返回true/false决定
        boolean ret = statisVOService.favor(sid,userInfo.getId());
        if(ret){
            statisVOService.addCommendScores(sid,1);
        }else {
            statisVOService.addCommendScores(sid,-1);
        }

        return new AjaxResult(ret, "").addData(statisVOService.getStatisVO(sid));
    }
    //攻略点赞
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo){

        if(userInfo == null){  //登录处理
            AjaxResult result  = new AjaxResult(false, "请先登录");
            result.setCode(102);  //102 事先约定好未登录状态码
            return result;
        }
        //点赞
        boolean ret = statisVOService.thumbup(sid, userInfo.getId());
        if(ret){
            statisVOService.addCommendScores(sid,1);
        }
        return new AjaxResult(ret, "").addData(statisVOService.getStatisVO(sid));
    }
}
